package com.pomr.controller.bind;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pomr.model.equipmentstorage.DeviceMatch;
import com.pomr.model.equipmentstorage.EquipmentStorageDetail;
import com.pomr.model.menu.EquipmentKind;
import com.pomr.model.menu.NetworkOperator;
import com.pomr.model.simstorage.SimStorageDetail;
import com.pomr.model.synthesize.Manufacturer;
import com.pomr.model.sys.User;
import com.pomr.service.bind.BindService;
import com.pomr.service.synthesize.ManufacturerService;
import com.pomr.util.common.CommonData;
import com.pomr.util.common.JWT;
import com.pomr.util.common.ResponseData;

/**
 * 
 * 类名称： BindController
 * 创建人： Administrator
 * 创建时间：2018年6月3日 下午4:33:17
 * 类描述：设备与sim卡绑定controller
 */
@Controller
@RequestMapping("/bind")
public class BindController {
	@Autowired
	@Qualifier("bindServiceImpl")
	private BindService bindService;
	@Autowired
	@Qualifier("manufacturerServiceImpl")
	private ManufacturerService manufacturerService;
	
	/**
	 * sim卡与设备绑定
	 * @param device
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/inter-device",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData bindEquipment(@RequestBody DeviceMatch device, HttpServletRequest request) throws Exception{
		ResponseData pd = null;
		User user = JWT.findUserByToken(request);
		if(bindService.bind(device, user)){
			pd = ResponseData.ok();
		}else{
			pd = ResponseData.custom("绑定失败");
		}
		return pd;
	}
	
	/**
	 * sim卡与设备绑定
	 * @param device
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/inter-sim",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData searchSim(@RequestBody Map<String,String> map) throws Exception{
		ResponseData pd = null;
		String id = map.get("id");
		if(id==null || id.equals("")){
			pd = ResponseData.custom("参数错误，未传递主键");
		}else{
			SimStorageDetail detail = bindService.searchBindSim(id);
			if(detail == null){//没有绑定信息
				pd = ResponseData.ok();
				//获取网络供应商
				List<NetworkOperator> nets = (List<NetworkOperator>) CommonData.data.get("NetworkOperator");
				pd.putDataValue("nets", nets);
				//获取厂商信息
				List<Manufacturer> manufacturers = manufacturerService.searchManufacturersOfAgency(null);
				pd.putDataValue("manufacturers", manufacturers);
			}else{
				pd = ResponseData.ok();
				pd.putDataValue("detail", detail);
			}
		}
		return pd;
	}
	
	/**
	 * 通过SIM卡解除绑定
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/inter-nodevice",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData noBindBySim(@RequestBody Map<String, String> map) throws Exception{
		ResponseData pd = null;
		String id = map.get("id");
		String type = map.get("type");
		if(id==null || id.equals("")){
			pd = ResponseData.custom("未上传SIM卡主键");
		}else{
			if(bindService.noBind(id, type)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.custom("解绑失败");
			}
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-searchsim",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData searchSimLd(@RequestBody Map<String,String> map, HttpServletRequest request) throws Exception{
		ResponseData pd = null;
		String network = map.get("network");
		String manufacturerid = map.get("manufacturer");
		User user  = JWT.findUserByToken(request);
		List<SimStorageDetail> details = bindService.searchSimLd(network, manufacturerid, user);
		pd = ResponseData.ok();
		pd.putDataValue("details", details);
		return pd;
	}

}
